package com.fyp.client.ptma.model;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;

import android.os.Environment;
import android.os.Parcel;
import android.os.Parcelable;

import com.fyp.client.ptma.utils.Device;

@Root(name = "version")
public class ApplicationVersion implements Parcelable
{
	@ElementList(name = "defects", required = false)
	private List<Defect> defects = new ArrayList<Defect>();

	@Element
	private ApplicationVersionPk versionPk;

	@Element(required = false)
	private String versionComment = "";

	@Element
	private boolean isUninstallPrevious = false;

	public String getOutputFolderPathForApk()
	{
		return Device.APP_FOLDER
				+ File.separator
				+ (versionPk.getApplication().getNameSpace() + getVersionPk()
						.getVersion()).hashCode();
	}

	public String getOutputFilePathForApk()
	{
		return Environment.getExternalStorageDirectory().getAbsolutePath()
				+ File.separator
				+ Device.APP_FOLDER
				+ File.separator
				+ (versionPk.getApplication().getNameSpace() + getVersionPk()
						.getVersion()).hashCode() + File.separator
				+ "application.apk";
	}

	public boolean isUninstallPrevious()
	{
		return isUninstallPrevious;
	}

	public void setUninstallPrevious(boolean isUninstallPrevious)
	{
		this.isUninstallPrevious = isUninstallPrevious;
	}

	public String getVersionComment()
	{
		return versionComment;
	}

	public void setVersionComment(String versionComment)
	{
		this.versionComment = versionComment;
	}

	public ApplicationVersionPk getVersionPk()
	{
		return versionPk;
	}

	public ApplicationVersion()
	{

	}

	public void setVersionPk(ApplicationVersionPk versionPk)
	{
		this.versionPk = versionPk;
	}

	public List<Defect> getDefects()
	{
		return defects;
	}

	public void setDefects(List<Defect> defects)
	{
		this.defects = defects;
	}

	public static final Parcelable.Creator<ApplicationVersion> CREATOR = new Parcelable.Creator<ApplicationVersion>()
	{
		public ApplicationVersion createFromParcel(Parcel in)
		{
			return new ApplicationVersion(in);
		}

		public ApplicationVersion[] newArray(int size)
		{
			return new ApplicationVersion[size];
		}
	};

	private ApplicationVersion(Parcel in)
	{

		versionPk = in.readParcelable(ApplicationVersionPk.class
				.getClassLoader());
		in.readTypedList(defects, Defect.CREATOR);
		versionComment = in.readString();

	}

	@Override
	public int describeContents()
	{

		return 0;
	}

	@Override
	public void writeToParcel(Parcel out, int flags)
	{

		out.writeParcelable(versionPk, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
		out.writeTypedList(defects);
		out.writeString(versionComment);

	}
}
